<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/11 19:10
 * Blog：www.myblogs.xyz
 */

namespace app\admin\controller;


use app\common\model\RoleMaps;
use think\facade\View;

class Role extends Common
{
    public function index()
    {
        $admin_id = input('param.admin_id', 0, 'intval');
        $list_where['admin_id'] = $this->adminId;
        if (!empty($admin_id)){
            $list_where['admin_id'] = $admin_id;
        }
        $role_model = new \app\common\model\Role();
        $data = $role_model->getLists($list_where, 10);
        // 获取副管理员身份
        $role_data = [];
        // 获取代理商人信息
        $admin_list = [];
        if ($this->adminData['role_id'] == 1){
            $admin_model = new \app\common\model\Admin();
            $admin_list = $admin_model->getList(['role_id'=>2]);
        }else{
            $role_data[] = $role_model->getFind(3);
        }

        View::assign([
            'admin_id'=>$admin_id,
            'admin_list'=>$admin_list,
            'count'=>$data['count'],
            'list'=>array_merge($role_data, $data['list']),
            'page'=>$data['page']
        ]);
        return View::fetch();
    }

    public function insertRole()
    {
        $this->saveRole();
        return View::fetch('save_role');
    }

    public function updateRole()
    {
        $this->saveRole();
        return View::fetch('save_role');
    }

    protected function saveRole()
    {
        $role_id = input('param.role_id', 0, 'intval');
        $role_model = new \app\common\model\Role();
        if (request()->isPost())
        {
            $data = input('post.');
            $data['admin_id'] = $this->adminId;
            if (!$role_model->operation($data)){
                $this->returnError($role_model->getError());
            }
            $this->returnSuccess('保存成功');
        }
        $oldData = $role_model->getFind($role_id);
        View::assign([
            'url'=>request()->url(),
            'role_id'=>$role_id,
            'oldData'=>$oldData,
        ]);
    }

    // Set access
    public function auth()
    {
        $role_id = input('param.role_id', 0, 'intval');
        $role_model = new \app\common\model\Role();
        $detail = $role_model->getFind($role_id);
        if (!empty($role_id) && !empty($detail)){
            $role_maps = new RoleMaps();
            $menu_model = new \app\common\model\Menu();
            if (request()->isPost())
            {
                $menu_ids = empty($_POST['menu_id'])? array() : $_POST['menu_id'];
                $role_maps->where(array('role_id'=>$role_id))->delete();
                foreach($menu_ids as $val){
                    if(!empty($val)){
                        $data = [
                            'role_id'=>$role_id,
                            'menu_id'=>(int)$val,
                        ];
                        $rows = $role_maps->insert($data);
                        if (empty($rows)){
                            $this->returnError('Authorization failure');
                        }
                    }
                }
                $this->returnSuccess('授权成功');
            }
            $menu_ids = $role_maps->getMenuIdsByRoleId($role_id);
            $menu_list = $menu_model->getList();
            if ($this->adminData['role_id'] != 1) {
                if ($this->adminData['menu_list']) {
                    // 非超级管理员可授权信息
                    $auth_menu = [];
                    foreach ($menu_list as $key=>$val){
                        if ($val['parent_id'] == 0) {
                            $subclass = [];
                            $subclass_count = 0;
                            foreach ($menu_list as $kk => $vv) {
                                if ($vv['parent_id'] == $val['menu_id']) {
                                    if (in_array($vv['menu_id'], $this->adminData['menu_list'])) {
                                        $sub_class = [];
                                        foreach ($menu_list as $k => $v) {
                                            if ($v['parent_id'] == $vv['menu_id']) {
                                                if (in_array($v['menu_id'], $this->adminData['menu_list'])) {
                                                    $sub_class[] = $v;
                                                }
                                            }
                                        }
                                        $vv['subclass'] = $sub_class;
                                        $subclass_count += count($sub_class);
                                        $subclass[] = $vv;
                                    }
                                }
                            }
                            $val['subclass'] = $subclass;
                            $val['subclass_count'] = $subclass_count;
                            if (!empty($subclass)){
                                $auth_menu[] = $val;
                            }
                        }
                    }
                    $menu_list = $auth_menu;
                }else {
                    $menu_list = [];
                }
            }else{
                $auth_menu = [];
                if (!empty($menu_list)){
                    if (is_array($menu_list)){
                        foreach ($menu_list as $key=>$val){
                            if ($val['parent_id'] == 0) {
                                $subclass = [];
                                $subclass_count = 0;
                                foreach ($menu_list as $kk => $vv) {
                                    if ($vv['parent_id'] == $val['menu_id']) {
                                        $sub_class = [];
                                        foreach ($menu_list as $k => $v) {
                                            if ($v['parent_id'] == $vv['menu_id']) {
                                                $sub_class[] = $v;
                                            }
                                        }
                                        $vv['subclass'] = $sub_class;
                                        $subclass_count += count($sub_class);
                                        $subclass[] = $vv;
                                    }
                                }
                                $val['subclass'] = $subclass;
                                $val['subclass_count'] = $subclass_count;
                                if (!empty($subclass)){
                                    $auth_menu[] = $val;
                                }
                            }
                        }
                    }
                }
                $menu_list = $auth_menu;
            }

            View::assign([
                'role_id'=>$role_id,
                'menu_ids'=>$menu_ids,
                'menu_list'=>$menu_list,
                'detail'=>$detail
            ]);

            return View::fetch();
        }
        return $this->redirect('/Role/index');
    }

    public function deleteRole()
    {
        $role_id = input('param.role_id',0,'intval');
        if (empty($role_id)){
            $this->redirect('/Role/index');
        }
        $role_model = new \app\common\model\Role();
        $rows = $role_model->where(['role_id'=>$role_id])->delete();
        if (empty($rows)){
            $this->returnError('删除失败');
        }
        $this->returnSuccess('删除成功', url('Role/index'));
    }
}